package games.flvPlayer
{
	import core.Global;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Video;
	
	import games.flvPlayer.control.ProgressControl;
	import games.flvPlayer.data.ContrlShowInfo;
	import games.flvPlayer.data.LoadedProgress;
	import games.flvPlayer.data.PlayProgress;
	import games.flvPlayer.data.PlayerShowInfo;
	import games.flvPlayer.data.StreamManager;
	import games.flvPlayer.ui.ShowControl;
	import games.flvPlayer.ui.ShowTime;
	import games.flvPlayer.ui.ShowVideo;
	import games.ninJump.ui.ScoreShow;
	
	import interfaces.IGame;
	import interfaces.IMsgListener;
	
	import module.Atom;
	
	import utils.components.ScrollBar;
	
	public class Player implements IGame, IMsgListener
	{
		public static var MAIN_UI:String = "flvPlayer/ui/player_skin.swf";
		private var m_contrl:Atom;
		private var m_video:Video;
		private var m_stream:StreamManager;
		private var m_playerInfo:PlayerShowInfo;
		private var m_contrlInfo:ContrlShowInfo;
		private var m_playProgress:PlayProgress;
		private var m_loadProgress:LoadedProgress;
		
		private var m_progControl:ProgressControl;
		
		private var m_showTime:ShowTime;
		private var m_contrlShow:ShowControl;
		private var m_showVideo:ShowVideo;
		private var m_voluem:ScrollBar;
		private var m_videoUrl:String ;
		
		public function Player()
		{
			trace("create a flv player...");
			m_videoUrl = "http://60.28.164.154/f/1/08e70646a97bd458a1a53c8433ede9ba56354609.hlv?vstr=8fc217f6c4f11d6a5f12c921b2fbc303&pid=352";
		}
		
		public function start():void
		{
			initVia();
			
			initData();
			initShowVideo();
			
			initControl();
			
			initShowTime();
			initVolume();
			
			
			initListener();
			
			onResizeStage();
		}
		
		public function destroy():void
		{
		}
		
		public function msgManager(e:*=null):void
		{
		}
		
		private function initData():void
		{
			m_playerInfo = new PlayerShowInfo();
			m_stream = new StreamManager(m_video);
			m_playProgress = new PlayProgress(m_stream);
			m_loadProgress = new LoadedProgress(m_stream);
		}
		
		private function initVia():void
		{
			m_video = new Video();
			Global.stage.addChild(m_video);
		}
		
		private function initControl():void
		{
			m_contrl = Global.loadResource(MAIN_UI,"bottom6");
			Global.stage.addChild(m_contrl);
			m_contrl.y = Global.stage.stageHeight - m_contrl.height;
			m_contrlInfo = new ContrlShowInfo(m_contrl);
			m_contrlShow = new ShowControl(m_contrl,m_contrlInfo);
			m_progControl = new ProgressControl(m_contrl.obj["mcProgressBar"]);
		}
		
		private function initShowVideo():void
		{
			m_showVideo = new ShowVideo(m_video,m_playerInfo);
			
		}
		
		private function initShowTime():void
		{
			m_showTime = new ShowTime(m_stream);
			m_showTime.ui = m_contrl.obj["txtTime"];
		}
		
		private function initVolume():void
		{
			m_voluem = new ScrollBar(m_contrl.obj["vol"]);
			m_voluem.addEventListener(Event.CHANGE,onChangeVol);
		}
		
		private function initListener():void
		{
			Global.stage.addEventListener(Event.RESIZE,onResizeStage);
			Global.stage.addEventListener(MouseEvent.MOUSE_MOVE,onRollOver);
			m_contrl.addEventListener(MouseEvent.CLICK,onClickContrl);
			m_playProgress.addEventListener(Event.CHANGE,onChangeProg);
			m_loadProgress.addEventListener(Event.CHANGE,onChangeLoaded);
			m_progControl.addEventListener(Event.CHANGE,onControlProg);
		}
		
		// --------------------监听器-------------------\\
		private function onResizeStage(e:Event = null):void
		{
			m_playerInfo.resize(Global.stage.stageWidth,Global.stage.stageHeight);
			m_contrlInfo.resize(Global.stage.stageWidth,Global.stage.stageHeight);
		}
		
		private function onRollOver(e:MouseEvent):void
		{
			m_contrlInfo.isShow = true;
			m_contrlInfo.resize(Global.stage.stageWidth,Global.stage.stageHeight);
		}
		
		private function onChangeVol(e:Event):void
		{
			m_stream.resetVolume(e.target.value);
		}
		
		private function onChangeProg(e:Event):void
		{
			var per:Number = e.target.value;
			var prog:* = m_contrl.obj["mcProgressBar"];
			//prog.mcPointer.x = (prog.bg.width - prog.mcPointer.width) * per;
			//prog.mcPlayBar.scaleX = per;
			m_progControl.value = per;
		}
		
		private function onChangeLoaded(e:Event):void
		{
			var per:Number = e.target.value;
			var prog:* = m_contrl.obj["mcProgressBar"];
			prog.mcLoadBar.scaleX = per;
		}
		
		private function onControlProg(e:Event):void
		{
			trace("contrl.......",e.target.value);
			/*
			a.addEventListener(MouseEvent.MOUSE_DOWN,
			function()
			{
				ns.seek((mouseX-a.x)/a.width*fulltime);
				if(if_playing==true){ns.pause();ns.resume();
			}
			});  
			times  ****** key fames******** 0,0,2.934,4.934,6.201,7.867,9.534,12.734,14.067,16.134,17.267,18.667,21.001,22.867,24.801,26.401,28.201,30.067,32.601,34.267,35.467,37.001,38.801,40.134,42.601,47.534
			filepositions  ****** key fames******** 1132,1211,121294,178031,221883,292562,349969,496332,544974,594312,636199,707214,793768,848786,917250,970935,1035969,1095272,1208814,1270430,1328610,1419617,1510921,1597225,1733656,1900460
			总时长： 47.534
			*/
			var per:Number = e.target.value;
			trace(m_loadProgress.value);
			/*if(m_loadProgress.value * 100 >= per)
				m_stream.seekPercent(per);
			else
			{
				m_stream.playPer(per);	
			}*/
			
			m_stream.playPer(per);
		}
		
		private function onClickContrl(e:MouseEvent):void
		{
			var name:String = e.target.name;
			switch(name)
			{
				case "btnPlay":
					m_stream.play(m_videoUrl);
					break;
				case "btnPause":
					m_stream.pause();
					break;
				case "btnStop":
					m_stream.stop();
					break;
			}
		}
	}
}